#include "problem.h"
#include<math.h>


//求分子分母的最大公约数
int gcb(int a,int b)
{
    if(b==0)
        return a;
    else
        return gcb(b,a%b);
}
//化简
Fraction reduction(Fraction &result)
{
    if(result.down < 0)  //分母为负
    {
        result.down = -result.down;
        result.up = - result.up;
    }
    else if(result.up == 0)  //分母为0
        result.down = 1;
    else
    {
        int x = gcb(abs(result.up),abs(result.down));  //分子分母同时除最大公约数
        result.up /= x;
        result.down /= x;
    }
    return result;
}
//加法
Fraction Add(Fraction a,Fraction b)
{
    Fraction c;
    c.up = a.up * b.down + a.down * b.up;
    c.down = a.down * b.down;
    return reduction(c);
}
//减法
Fraction minu(Fraction a,Fraction b)
{
    Fraction c;
    c.up = a.up * b.down - a.down * b.up;
    c.down = a.down * b.down;
    return reduction(c);
}
//乘法
Fraction multi(Fraction a,Fraction b)
{
    Fraction c;
    c.up = a.up * b.up;
    c.down = a.down * b.down;
    return reduction(c);
}
//除法
Fraction divide(Fraction a,Fraction b)
{
    Fraction c;
    c.up = a.up * b.down;
    c.down = a.down * b.up;
    return reduction(c);
}
problem::problem(QObject *parent) : QObject(parent)
{
    std::srand((unsigned)time(NULL));
}
